package com.yonyou.pmclouds.milestone.processor;

import com.yonyou.pmclouds.basecom.bp.processor.MultiProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.milestone.entity.MilestoneVO;
import com.yonyou.pmclouds.milestone.mapper.MilestoneMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author th
 * @date 2019/11/6 16:47
 */
@Component
public class CheckCodeAndNameMultiProcessor implements MultiProcessor<MilestoneVO> {
    @Autowired
    private MilestoneMapper mapper;

    @Override
    public void process(MilestoneVO[] vos, MilestoneVO[] origs) throws BusinessException {
        if (ArrayUtils.isEmpty(vos))
            return;

        List<String> codeList = new ArrayList<>(), nameList = new ArrayList<>();
        for (MilestoneVO vo : vos) {
            if (vo.getCode().length() > 20)
                throw new BusinessException("'" + vo.getCode() + "'" + "编码长度不能超过20");

            if (vo.getName().length() > 50)
                throw new BusinessException("'" + vo.getName() + "'" + "名称长度不能超过50");

            int count = mapper.countCode(vo.getCode(), vo.getPkTenant());
            if (count > 0 || codeList.contains(vo.getCode()))
                throw new BusinessException("'" + vo.getCode() + "'" + "编码已被占用，请重新设置");

            count = mapper.countName(vo.getName(), vo.getPkTenant());
            if (count > 0 || nameList.contains(vo.getName()))
                throw new BusinessException("'" + vo.getName() + "'" + "名称已被占用，请重新设置");

            codeList.add(vo.getCode());
            nameList.add(vo.getName());
        }
    }
}
